package StudyCode.summary.BufferAndChannel;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.EnumSet;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class UsingFileChannelWriteToTextFile {
	public static void main(String[] args) {
		Path path = Paths.get("E:\\Copy", "1.txt");
		ByteBuffer buffer = ByteBuffer.wrap("Using FileChannel Write To Text File".getBytes());

		try (FileChannel fileChannel = (FileChannel.open(path,
				EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE)))) {

			fileChannel.position(0);
			fileChannel.write(buffer);
			System.out.println("使用 FileChannel 类写入到文本文件成功");
		} catch (IOException ex) {
			System.err.println(ex);
		}
	}
}
